﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KerriganDataContract;
using System.Diagnostics;
using KerriganDataContract.OfficialTest;
using System.Data.Objects;

namespace KerriganService.DAC
{
    public class ExamDAC
    {
        public static List<OfficialTestDC> GetCustomerExams(int customerId){
            List<OfficialTestDC> list = new List<OfficialTestDC>();
            using (KerriganDBEntities db = new KerriganDBEntities()){
                try{
                    list = CopyListEntityToListBo((from examApplicant in db.TOfficialTestApplicants
                    where examApplicant.IdCustomerFK == customerId
                    select examApplicant.TOfficialTest).ToList());
                }
                catch(Exception e){
                
                }
            }
            
            return list;
        }

        public static List<OfficialTestDC> GetAllExams()
        {
            List<OfficialTestDC> list = new List<OfficialTestDC>();
            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    list = CopyListEntityToListBo(db.TOfficialTests.ToList());
                }
                catch (Exception e)
                {

                }
            }

            return list;
        }

        public static List<ExamsByCustomer_Result> GetAllExamsAgencyByCustomer(int customerId)
        {
            List<ExamsByCustomer_Result> list = new List<ExamsByCustomer_Result>();
            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    list = db.ExamsByCustomer(customerId).ToList();
                }
                catch (Exception e)
                {

                }
            }

            return list;
        }

        public static Boolean AddExamToCustomer(int examId, int customerId){
            Boolean result = false;
            using (KerriganDBEntities db = new KerriganDBEntities()){
                try{
                    TOfficialTestApplicant entity = new TOfficialTestApplicant();
                    entity.IdOfficialTestFK = examId;
                    entity.IdCustomerFK = customerId;
                    db.TOfficialTestApplicants.AddObject(entity);
                    db.SaveChanges();
                    result = true;
                }
                catch(Exception e){
                
                }
            }
            return result;
            
        }

        public static int DeleteExam(int idExam, int IdCustomer){
            int result = 0;
            using (KerriganDBEntities db = new KerriganDBEntities()){
                try{
                    TOfficialTestApplicant entity = new TOfficialTestApplicant();
                    entity = db.TOfficialTestApplicants.FirstOrDefault(f => f.IdOfficialTestFK == idExam && f.IdCustomerFK == IdCustomer);
                    db.TOfficialTestApplicants.DeleteObject(entity);
                    db.SaveChanges();
                    result = idExam;
                }
                catch(Exception e){
                
                }
            }
            return result;
        }




        public static OfficialTestDC CopyEntityToBo(TOfficialTest entity)
        {
            OfficialTestDC bo = new OfficialTestDC();

            try
            {
                bo.Id = entity.IdOfficialTest;
                bo.Date = entity.Date;
                bo.Title = entity.Title;
                bo.MaxApplicant = entity.MaxApplicant;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }

        public static List<OfficialTestDC> CopyListEntityToListBo(List<TOfficialTest> entities)
        {
            List<OfficialTestDC> bo = new List<OfficialTestDC>();
            try
            {
                foreach (TOfficialTest entity in entities)
                {
                    bo.Add(CopyEntityToBo(entity));
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }
        public static TOfficialTest CopyBoToEntity(OfficialTestDC bo)
        {
            TOfficialTest entity = new TOfficialTest();

            try
            {
                entity.IdOfficialTest = bo.Id;
                entity.Date = bo.Date;
                entity.Title = bo.Title;
                entity.MaxApplicant = bo.MaxApplicant;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return entity;
        }
    }
}